import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx';
// *********************************** 路径配置新增 start  
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import pxtoViewPort from 'postcss-px-to-viewport'
import unoCss from 'unocss/vite'
import { presetIcons, presetAttributify, presetUno } from 'unocss'
import { VitePWA } from "vite-plugin-pwa"
import {visualizer} from "rollup-plugin-visualizer"
const pathResolve = (dir: string): any => {
  return resolve(__dirname, ".", dir)
}

const alias: Record<string, string> = {
  '@': pathResolve("src")
}
 
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), vueJsx(), AutoImport({
    imports: ['vue'],
    dts: "src/auto-import.d.ts"
  }), unoCss({
    presets: [presetIcons(), presetAttributify(), presetUno()],
    rules:[
      ['flex', { display: 'flex' }],
      ['red', { color: 'red' }],
      [/^m-(\d+)$/, ([, d]) => ({ margin: `${Number(d) * 10}px` })]
    ],
    shortcuts:{
      fuheng:['flex','red']

    }
  }), visualizer({ open: false }), VitePWA({
    workbox: {
      cacheId: "XIaoman",//缓存名称
      runtimeCaching: [
        {
          urlPattern: /.*\.js.*/, //缓存文件
          handler: "StaleWhileRevalidate", //重新验证时失效
          options: {
            cacheName: "XiaoMan-js", //缓存js，名称
            expiration: {
              maxEntries: 30, //缓存文件数量 LRU算法
              maxAgeSeconds: 30 * 24 * 60 * 60 //缓存有效期

            }
          }
        }
      ]
    },
  })],
  css:{
    postcss:{
      plugins:[
        // pxtoViewPort({
        //  unitToVonvurt:'px',
        //  viewportWidth:320
        // })
      ] 
    }
  },
  base: './' ,// 添加这个属性
  resolve: {  // ****************** 路径配置新增
    alias     // ****************** 路径配置新增
  },
  build:{
    chunkSizeWarningLimit:2000,
    cssCodeSplit:true,
    sourcemap:false,
    minify:'terser',
    assetsInlineLimit:4000
  }
})
  